package com.zzty.zzlp.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.zzty.zzlp.entity.Question;
import com.zzty.zzlp.entity.TeamExamTransation;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/que")
public class QusetionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private UserService userService;
    @Autowired
    private TeamExamTransationService teamExamTransationService;
    @Autowired
    private ClassGroupService classGroupService;

    @RequestMapping("/AssginTask")
    @ResponseBody

    public int show(HttpServletRequest request, Model model) {
        System.out.println("start");

        List<Question> que = this.questionService.selectAll(0, 10);
        Integer Num = this.questionService.count();

        System.out.println(Num);

        double num = (double) Num / (double) 10;
        if (num > Num / 10) {
            num = Num / 10 + 1;
        } else {
            num = Num / 10;

        }
        System.out.println(num);

        return (int) num;
    }

    @RequestMapping("/fenye")
    @ResponseBody
    public List<Question> fenye(HttpServletRequest request, Model model) {
        // System.out.println("start");

        int num = Integer.parseInt(request.getParameter("num"));
        // System.out.println(num);

        List<Question> que = this.questionService.selectAll(num, 10);

        return que;
    }

    @RequestMapping("/insert")
    @ResponseBody

    public void insert(HttpServletRequest request, Model model) {

        String insert = request.getParameter("insert");
        System.out.println(insert);
        if ("insertself".equals(insert)) {

            System.out.println("insert");
            // int age = Integer.parseInt(request.getParameter("id"));
            String content = request.getParameter("content");
            String Questions = request.getParameter("Questions");
            String[] con = content.split("-");
            String ct = "";
            for (String c : con) {
                if (c.length() > 1) {

                    // ����
                    this.questionService.insertQue(c);
                    // ��id
                    Integer id = this.questionService.selectQueId(c);
                    ct = ct + id + "|";
                }
            }
            Questions = ct + Questions;

            //--------------------------------------------
            System.out.println("self");
            String date = request.getParameter("date");
            String questions = Questions;

            User user = (User) request.getSession().getAttribute("User");
            Integer clazzId = user.getClazzId();

            //Integer clazzId = 4;
            this.questionService.searchNewId(date, questions, clazzId);
            Integer selfid = this.questionService.selectLastSelfId(clazzId);

            List<Integer> stu = this.userService.searchStuIdByClazzID(clazzId);

            for (Integer s : stu) {
                this.questionService.insertselfexamtransation(selfid, s);
            }
        } else if ("insertteam".equals(insert)) {
            System.out.println("insert");
            // int age = Integer.parseInt(request.getParameter("id"));
            String content = request.getParameter("content");
            String Questions = request.getParameter("Questions");
            String[] con = content.split("-");
            String ct = "";
            for (String c : con) {
                if (c.length() > 1) {
                    System.out.println(c);
                    this.questionService.insertQue(c);

                    Integer id = this.questionService.selectQueId(c);
                    ct = ct + id + "|";
                }
            }
            Questions = ct + Questions;

            System.out.println(Questions);
            System.out.println("team");
            String date = request.getParameter("date");
            String questions = Questions;

            User user = (User) request.getSession().getAttribute("User");
            Integer clazzId = user.getClazzId();

            //Integer clazzId = 4;
            this.questionService.searchNewId1(date, questions, clazzId);
            Integer selfid = this.questionService.selectLastTeamId(clazzId);

            Integer num = this.classGroupService.count(clazzId);
            System.out.println(num);
            double testNum = (double) num / (double) 2;
            if (testNum > num / 2) {
                num = num / 2 + 1;

            } else {
                num = num / 2;

            }
            List<String> stu = this.questionService.selectstu_ids(num, clazzId);
            // addTeamExamTransation
            TeamExamTransation tet = new TeamExamTransation();
            tet.setExamId(selfid);
            for (String s : stu) {
                tet.setUserIds(s);

                this.teamExamTransationService.addTeamExamTransation(tet);
            }
        }

    }


}
